package com.qf.service;

import com.qf.constant.RabbitMQConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 实现路由过滤策略
 * <p>
 * "phase,black,limitFiveMinutes,dirtyWords,fee,router".split(,)
 */
@Service("router")
@Slf4j
public class RouterFilterServiceImpl implements FilterChainService {

    @Autowired
    private CacheFeign cacheFeign;
    @Autowired
    private AmqpAdmin amqpAdmin;   //动态创建队列
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 实现路由过滤策略
     *
     * @param standardSubmit
     * @return
     */
    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        System.out.println("======实现路由过滤策略====");
/*
      路由 ：从客户通道表中选择所配的通道下发
           如果找不到通道把发送日志存到Elasticsearch
           如果同时是HTTP的发送方式就模拟出状态报告推送给客户
*/
        //1.队列的名称
        String queueName = RabbitMQConstants.TOPIC_SMS_GATEWAY + standardSubmit.getGatewayID();
        //2.动态创建指定的队列
        amqpAdmin.declareQueue(QueueBuilder.durable(queueName).build());
        //3.发送消息到指定队列（使用默认的交换机）
        rabbitTemplate.convertAndSend(queueName,standardSubmit);
        //4.记录日志
        log.info("[策略模块 - 路由策略] 路由策略执行成功！！！消息发送到了网关模块对应的队列");
        System.out.println("[策略模块 - 路由策略] 路由策略执行成功！！！消息发送到了网关模块对应的队列");
        return true;
    }
}
